package com.efuture.androidmvpdemo.ui.register

import com.efuture.androidmvpdemo.login.LoginInteractor
import com.efuture.androidmvpdemo.net.ApiParameter
import com.efuture.androidmvpdemo.net.NetService
import com.efuture.netrequestdemo.bean.User
import com.efuture.netrequestdemo.utils.AppUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**

 *@description:TODO
 * @author: zhaoshanshan
 * @date: 2022/3/26 5:53 下午

 */
 class RegisterInteractor {

    interface OnRegisterFinishedListener {

        fun onSuccess()
        fun onFail(reason:String)
    }

    fun registerUser(mainScope:CoroutineScope,username: String, password: String, listener: RegisterInteractor.OnRegisterFinishedListener){
        if(username.isEmpty()){
            listener.onFail("用户名不能为空")
            return
        }
        if(password.isEmpty()){
            listener.onFail("密码名不能为空")
            return
        }
        //开始调用网络
        mainScope.launch(Dispatchers.Main) {
            //登录
            val loginUser: User? = NetService.request(ApiParameter.registerUser(username,password,password),"正在注册中...")
            if(null!=loginUser&&loginUser.errorCode==0){
                listener.onSuccess()
            }else{
                if(null!=loginUser?.errorMsg){
                    listener.onFail(loginUser.errorMsg)

                }else{
                    listener.onFail("注册失败")
                }
            }
        }
    }
}